//
//  ModifyPWDViewController.m
//  QRMaster
//
//  Created by myApple on 15/4/16.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "ModifyPWDViewController.h"
#import "LineView.h"
#import "RequestNetWork.h"
#import "MyRsa.h"
#import "PromptInfoView.h"
#import "Reachability.h"

@interface ModifyPWDViewController ()<UITextFieldDelegate>
{
    UITextField * oldTextField;
    UITextField * newTextField;
    UITextField * againTextField;
    NSUserDefaults * userInfo;
    
    UIImageView * oldPWDView;
    UIImageView * NewPDView;
    UIImageView * AngainPWDView;
    
    UILabel * titleLabel;
    UILabel * oldPwdLabel;
    UILabel * newPwdLabel;
    UILabel * againPwdLabel;
    UIButton * makeSureBtn;
}

@end

@implementation ModifyPWDViewController

- (void)viewDidLoad {
//    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    self.view.backgroundColor = UIColorFromRGB(0xebebeb);
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
    [self prepareView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    titleLabel.text = NSLocalizedString(@"modifyPWD", nil);
    oldPwdLabel.text = NSLocalizedString(@"oldLoginPWD", nil);
    oldTextField.placeholder = NSLocalizedString(@"inputLoginPWD", nil);
    newPwdLabel.text = NSLocalizedString(@"newPassWord", nil);
    newTextField.placeholder = NSLocalizedString(@"loginingPromt", nil);
    againPwdLabel.text = NSLocalizedString(@"makeSurePWD", nil);
    againTextField.placeholder = NSLocalizedString(@"inputAgainLogin", nil);
    [makeSureBtn setTitle:NSLocalizedString(@"alterSure", nil) forState:0];
    self.navigationItem.title=NSLocalizedString(@"modifyPWD", nil);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)prepareView
{
    float x = 0;
    float y = 16;
    float width = UI_SCREEN_WIDTH;
    float height = 44;
    
    x = 0;
    y = 16;
    height = 44;
    width = UI_SCREEN_WIDTH;
    UIView * passWordView = [[UIView alloc]initWithFrame:CGRectMake(x, y, UI_SCREEN_WIDTH, height)];
    passWordView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:passWordView];
    
    x = 16;
    y = 14;
    height = 16;
    width = 114;
    oldPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    oldPwdLabel.text = NSLocalizedString(@"oldLoginPWD", nil);
    oldPwdLabel.font = [UIFont systemFontOfSize:13.0];
    oldPwdLabel.textColor = UIColorFromRGB(0x333333);
    oldPwdLabel.textAlignment = NSTextAlignmentLeft;
    [passWordView addSubview:oldPwdLabel];
    
    x = 130;
    y = 14;
    width = UI_SCREEN_WIDTH - x;
    height = 16;
    oldTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    oldTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"inputLoginPWD", nil) attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x727272)}];
    oldTextField.font = [UIFont systemFontOfSize:13.0];
    oldTextField.textColor = UIColorFromRGB(0x333333);
    oldTextField.secureTextEntry = YES;
    oldTextField.delegate = self;
    [passWordView addSubview:oldTextField];

    
    x = 0;
    y = passWordView.frame.size.height + passWordView.frame.origin.y + 16;
    height = 88.5;
    width = UI_SCREEN_WIDTH;
    UIView * newPassWordView = [[UIView alloc]initWithFrame:CGRectMake(x, y, UI_SCREEN_WIDTH, height)];
    newPassWordView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:newPassWordView];
    
    x = 16;
    y = 14;
    height = 16;
    width = 114;
    newPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    newPwdLabel.text = NSLocalizedString(@"newPassWord", nil);
    newPwdLabel.textColor = UIColorFromRGB(0x333333);
    newPwdLabel.font = [UIFont systemFontOfSize:13.0];
    newPwdLabel.textAlignment = NSTextAlignmentLeft;
    [newPassWordView addSubview:newPwdLabel];
    
    x = 130;
    y = 14;
    width = UI_SCREEN_WIDTH - x;
    height = 16;
    newTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    newTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"loginingPromt", nil) attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x727272)}];
    newTextField.font = [UIFont systemFontOfSize:13.0];
    newTextField.textColor = UIColorFromRGB(0x333333);
    newTextField.secureTextEntry = YES;
    newTextField.delegate = self;
    [newPassWordView addSubview:newTextField];

    
    x = 16;
    y = 44;
    height = 0.5;
    width = UI_SCREEN_WIDTH - 16;
    LineView * line_ = [[LineView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    line_.backgroundColor = UIColorFromRGB(0xc1c1c1);
    [newPassWordView addSubview:line_];
    
    x = 16;
    y = 58.5;
    height = 16;
    width = 114;
    againPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    againPwdLabel.text = NSLocalizedString(@"makeSurePWD", nil);
    againPwdLabel.textColor = UIColorFromRGB(0x333333);
    againPwdLabel.font = [UIFont systemFontOfSize:13.0];
    againPwdLabel.textAlignment = NSTextAlignmentLeft;
    [newPassWordView addSubview:againPwdLabel];
    
    x = 130;
    y = 44+14+0.5;
    width = UI_SCREEN_WIDTH - x;
    height = 16;
    againTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    againTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"inputAgainLogin", nil) attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x727272)}];
    againTextField.font = [UIFont systemFontOfSize:13.0];
    againTextField.textColor = UIColorFromRGB(0x333333);
    againTextField.secureTextEntry = YES;
    againTextField.delegate = self;
    [newPassWordView addSubview:againTextField];
    

    x = 16;
    width = UI_SCREEN_WIDTH - 2*x;
    height = 48;
    y = newPassWordView.frame.size.height + newPassWordView.frame.origin.y + 38;
    makeSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    makeSureBtn.frame = CGRectMake(x, y, width, height);
    makeSureBtn.layer.cornerRadius = 5.0;
    makeSureBtn.layer.masksToBounds = YES;
    makeSureBtn.titleLabel.font = [UIFont systemFontOfSize:19.0];
    [makeSureBtn setTintColor:[UIColor whiteColor]];
    [makeSureBtn setTitle:NSLocalizedString(@"alterSure", nil) forState:0];
    [makeSureBtn setBackgroundColor:RGBA(135, 199, 66, 1)];
    [makeSureBtn addTarget:self action:@selector(makesureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:makeSureBtn];
}

-(void)makesureClick
{
    [oldTextField resignFirstResponder];
    [newTextField resignFirstResponder];
    [againTextField resignFirstResponder];
    
    if (oldTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"OriginalPsw", nil)];
        return;
    }
    if (newTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"NewPsw", nil)];
        return;
    }
    if (againTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"ComfirmPsw", nil)];
        return;
    }
    if (![newTextField.text isEqualToString:againTextField.text]) {
        [PromptInfoView showMessage:NSLocalizedString(@"differentPWD", nil)];
        return;
    }
    
    if ([oldTextField.text isEqualToString:newTextField.text]) {
        [PromptInfoView showMessage:NSLocalizedString(@"commonPWD", nil)];
        return;
    }
    
    if (![self validatePassword:newTextField.text]) {
        [PromptInfoView showMessage:NSLocalizedString(@"passPromt", nil)];
        return;
    }
    
    makeSureBtn.enabled = NO;//防止多次点击
    NSString * oldpassword = [self getPassword:oldTextField.text];
    NSString * newpassword = [self getPassword:newTextField.text];
    userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:oldpassword forKey:@"oldPasswd"];
    [postDict setValue:newpassword forKey:@"newPasswd"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(MODIFYPASSWORD) params:postDict block:^(id downLoadData, id error) {
        makeSureBtn.enabled = YES;
        if ([downLoadData objectForKey:@"data"] != nil)
        {
            NSString * token = [[downLoadData objectForKey:@"data"] objectForKey:@"token"];
            [userInfo setValue:token forKey:@"token"];
            [userInfo synchronize];
            [PromptInfoView showMessage:NSLocalizedString(@"modifySuccess", nil)];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else if (error != nil)
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        
    }];
 
}

-(NSString *)getPassword:(NSString *)password
{
    MyRsa * rsa = [[MyRsa alloc]init];
    if (rsa != nil) {
        NSString * encryptPWD = [rsa RSAEncrypotoTheData:[NSString stringWithFormat:@"12345678%@",password] key:publicKeyString];
        encryptPWD = [encryptPWD stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return encryptPWD;
    }
    else
        return nil;
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == oldTextField) {
        oldPWDView.image = [UIImage imageNamed:@"36密码可见"];
        oldTextField.secureTextEntry = YES;
    }
    else if (textField == newTextField)
    {
        NewPDView.image = [UIImage imageNamed:@"36密码可见"];
        newTextField.secureTextEntry = YES;
    }
    else if(textField == againTextField && UI_SCREEN_HEIGHT == 480)
    {
        AngainPWDView.image = [UIImage imageNamed:@"36密码可见"];
        againTextField.secureTextEntry = YES;
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 40, self.view.frame.size.width, self.view.frame.size.height);
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == oldTextField) {
        oldPWDView.image = [UIImage imageNamed:@"36密码不可见"];
    }
    else if (textField == newTextField)
    {
        NewPDView.image = [UIImage imageNamed:@"36密码不可见"];
    }
    else if(textField == againTextField && UI_SCREEN_HEIGHT == 480)
    {
        AngainPWDView.image = [UIImage imageNamed:@"36密码不可见"];
        self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 40, self.view.frame.size.width, self.view.frame.size.height);
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self hiddenKeyboard];
    return YES;
}


//密码验证
- (BOOL) validatePassword:(NSString *)PassWord
{
    NSString *passWordRegex = @"(?=^[a-zA-Z0-9]{6,20}$).*[a-zA-Z]+.*";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:PassWord];
}

-(void)hiddenKeyboard
{
    [oldTextField resignFirstResponder];
    [newTextField resignFirstResponder];
    [againTextField resignFirstResponder];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}




@end
